import math
movie_data = {"宝贝当家": [45, 2, 9, "喜剧片"],
              "美人鱼": [21, 17, 5, "喜剧片"],
              "澳门风云3": [54, 9, 11, "喜剧片"],
              "功夫熊猫3": [39, 0, 31, "喜剧片"],
              "谍影重重": [5, 2, 57, "动作片"],
              "叶问3": [3, 2, 65, "动作片"],
              "伦敦陷落": [2, 3, 55, "动作片"],
              "我的特工爷爷": [6, 4, 21, "动作片"],
              "奔爱": [7, 46, 4, "爱情片"],
              "夜孔雀": [9, 39, 8, "爱情片"],
              "代理情人": [9, 38, 2, "爱情片"],
              "新步步惊心": [8, 34, 17, "爱情片"]}
movie_data.items()

movie_data.keys()

movie_data.values()

KNN=[]
x=[23,3,17,'?片']
for key,value in movie_data.items():
    d=math.sqrt(((x[0] - value[0]) ** 2 + (x[1] - value[1]) ** 2 + (x[2] - value[2]) ** 2))
    KNN.append([key, round(d, 2)])

print(KNN)

KNN.sort(key=lambda dis:dis[1])
KNN=KNN[:4]
labels={"喜剧片":0,"动作片":0,"爱情片":0}
for i in KNN:
    label=movie_data[i[0]]
    labels[label[3]]+=1
labels=sorted(labels.items(),key=lambda x:x[1],reverse=True)
print(labels,labels[0][0],sep='\n')


'''
    按照键值
    KNN.sort(key=lambda x:x[1])
    按照键
    KNN.sort(key=lambda x:x[0]
    1、基于cmp

L = [(‘a’, 3), (‘d’, 2), (‘c’, 1), (‘b’, 4)]
a = sorted(L, cmp=lambda x, y : cmp(x[0], y[0]))
b = sorted(L, cmp=lambda x, y : cmp(x[1], y[1]))
print L
print a
print b
输出：
[(‘a’, 3), (‘d’, 2), (‘c’, 1), (‘b’, 4)]
[(‘a’, 3), (‘b’, 4), (‘c’, 1), (‘d’, 2)]
[(‘c’, 1), (‘d’, 2), (‘a’, 3), (‘b’, 4)]

2、基于key
L = [(‘a’, 3), (‘d’, 2), (‘c’, 1), (‘b’, 4)]
a = sorted(L, key=lambda x : x[0])
b = sorted(L, key=lambda x : x[1])
print L
print a
print b
输出：
[(‘a’, 3), (‘d’, 2), (‘c’, 1), (‘b’, 4)]
[(‘a’, 3), (‘b’, 4), (‘c’, 1), (‘d’, 2)]
[(‘c’, 1), (‘d’, 2), (‘a’, 3), (‘b’, 4)]

3、基于reverse
L = [2, 1, 4, 9, 6]
a = sorted(L, reverse=True)
b = sorted(L, reverse=False)
print L
print a
print b
输出：

[2, 1, 4, 9, 6]
[9, 6, 4, 2, 1]
[1, 2, 4, 6, 9]

'''


